package com.fw.ui.bao;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeoutException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.EntryBean;
import com.fw.service.property.PropertyService;
import com.fw.ui.service.ServiceExecutor;
import com.fw.ui.service.ServiceOrder;

@Component
@Scope(SCOPE_PROTOTYPE)
public class PropertyBAO extends AbstractBAO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 7250064492225378259L;

    @Autowired
    protected PropertyService propertyService;

    private static Map<String, String> propertiesMap = new TreeMap<String, String>();

    @SuppressWarnings("unchecked")
    public boolean initialSystemProperties() {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.propertyService);
	    serviceOrder.setMethodName("getPropertyAll");
	    serviceOrder.setParameterTypes(new Class[] {});
	    serviceOrder.setParameterValues(new Object[] {});

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<EntryBean<String, String>> entryBeans = (List<EntryBean<String, String>>) serviceOrder.getReturnValue();
		if (entryBeans != null && !entryBeans.isEmpty()) {
		    for (EntryBean<String, String> entry : entryBeans) {
			propertiesMap.put(entry.getKey(), entry.getValue());
		    }
		    return true;
		} else {
		    return false;
		}
	    }
	    return false;
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return false;
    }

    public synchronized String getPropertyValue(String key) {
	String value = null;
	if (!propertiesMap.isEmpty()) {
	    value = propertiesMap.get(key);
	}
	return value;
    }

    public String getPropertyWithDefaultValue(String key, String defaultValue) {
	String value = null;
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.propertyService);
	    serviceOrder.setMethodName("getPropertyWithDefaultValue");
	    serviceOrder.setParameterTypes(new Class[] { String.class, String.class });
	    serviceOrder.setParameterValues(new Object[] { key, value });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		String result = (String) serviceOrder.getReturnValue();
		if (result != null) {
		    propertiesMap.put(key, value);
		}
	    }
	    return value;
	} catch (TimeoutException e) {
	    e.printStackTrace();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return value;
    }

    @SuppressWarnings("unchecked")
    public Map<String, String> selectPropertyByPrefixKey(String prefixKey) {
	Map<String, String> valueMap = new HashMap<String, String>();
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.propertyService);
	    serviceOrder.setMethodName("selectPropertyByPrefixKey");
	    serviceOrder.setParameterTypes(new Class[] { String.class });
	    serviceOrder.setParameterValues(new Object[] { prefixKey });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<EntryBean<String, String>> entryBeans = (List<EntryBean<String, String>>) serviceOrder.getReturnValue();
		if (entryBeans != null && !entryBeans.isEmpty()) {
		    for (EntryBean<String, String> entry : entryBeans) {
			valueMap.put(entry.getKey(), entry.getValue());
		    }
		}
	    }
	    return valueMap;
	} catch (TimeoutException e) {
	    e.printStackTrace();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return valueMap;
    }

    public Boolean setPropertyValue(String key, String value) {
	if (key == null) {
	    return false;
	}
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.propertyService);
	    serviceOrder.setMethodName("setPropertyValue");
	    serviceOrder.setParameterTypes(new Class[] { String.class, String.class });
	    serviceOrder.setParameterValues(new Object[] { key, value });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		Boolean resultBol = (Boolean) serviceOrder.getReturnValue();
		if (resultBol != null && resultBol.booleanValue()) {
		    propertiesMap.put(key, value);
		}
	    }
	    return true;
	} catch (TimeoutException e) {
	    e.printStackTrace();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return false;
    }

}
